
$(document).ready(function(){
	$(".comments").hide();

	function addFavor(){
		//add into favor
	};

	$(".feedback a").click(function(){
		var val = 1;
		var span = '看涨';
		if ($(this).attr("class") == 'j_down')
		{
			val = -1;
			span = '看跌';
		}else if ($(this).attr('class') == 'j_comment')
		{
			val = 0;
			span = '评论';
		}else if ($(this).attr('class') == 'j_favor')
		{
			addFavor();
			return false;
		}
		
		var rankBody = $(this).parents(".body");
		$(".comments span", rankBody).replaceWith("<span>" + span + ":</span>");
		$("input[type=hidden]", rankBody).attr("value", val);
		$(".comments", rankBody).show('slow', function(){
			$(".comments textarea", rankBody).focus();
			$(".comments .buttons input", rankBody).removeAttr("disabled");
		});

		return false;
	});


	$('.j_cancelcomment').click(function(){
		$(this).parents(".comments").hide('slow');
		return false;
	});

	$('.comments textarea').keyup(function(event){
		if ($(this).attr("rel") == 'multi'){
			return;
		}
		if (event.keyCode == '13')
		{
			$(this).parents(".comments").children(".j_submit").click();
			return;
		}
		if( $(this).val().length * $(this).val().fontsize() >= $(this).width()){
			alert("inner width:" + $(this).val().length);
			alert("width:" + $(this).width());
			$(this).height($(this).height() * 3).attr("rel", "multi");
		}
	});
/**
	$(".j_down").click(function(){
		var rankBody = $(this).parents(".body");
		var text = $(".text", rankBody);
		text.html(text.html().replace(/.*:/, '看跌:'));
		$(".comments", rankBody).slideToggle("slow", function(){$(".comments textarea", rankBody).focus();});
	});
*/
	/**********************************************************/
	$(".j_submit").click(function(){
		form = $(this).parents("form");
		submit = $(this).attr("disabled", "disabled");
		text = $('textarea', form).attr("disabled", "disabled");
		msg = $('.text textarea', form).val();
		$.post("http://localhost/jquery/success.php", {message : msg}, function(xml){
			submit.removeAttr("disabled");
			text.removeAttr("disabled");
			showMessage(xml)
		});
		return false;
	});

	function showMessage(xml){
		alert(xml);
		if( $('status', xml).text() != "OK" ){
			alert($('status', xml).text()); 
			return;
		}
		$('item', xml).each(function(id){
			item = $('item', xml).get(id);
			alert($('key', item).text() + " = " + $('val', item).text());
		});
	}
});